package com.karry.yl.web.services;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.JsonResponse;
import com.karry.commons.vo.LayTableResponse;
import com.karry.yl.domain.services.Items;
import com.karry.yl.domain.services.Meal;
import com.karry.yl.domain.services.MealItems;
import com.karry.yl.service.services.ItemsService;
import com.karry.yl.service.services.MealItemsService;
import com.karry.yl.service.services.MealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * 服务套餐Controller
 */
@Controller
@RequestMapping("/meal")
public class MealController extends SuperController {

    @Autowired
    private MealService mealService;
    @Autowired
    private MealItemsService mealItemsService;
    @Autowired
    private ItemsService itemsService;

    @RequestMapping
    public String listUI(){
        return "services/meal/list";
    }

    @RequestMapping("/add")
    public String addUI(ModelMap map){
        List<Items> items = itemsService.selectList(new EntityWrapper<Items>().eq("orgId", currentUser.getOrgId()).eq("deleted", false));  //所有的服务项目
        map.put("items",items);
        if(currentUser.getRoleId()==1){//民办
            map.put("price",1);
        }else{
            map.put("price",0);
        }
        return "services/meal/add";
    }

    @RequestMapping("edit")
    public String editUI(Long id, ModelMap map){
        Meal meal = mealService.selectById(id);
        List<MealItems> mealItems = mealItemsService.selectList(new EntityWrapper<MealItems>().eq("mealId", meal.getId()));
        List<Long> mealItemsIds = new ArrayList<>();
        for(MealItems m :mealItems){
            mealItemsIds.add(m.getItemsId());
        }
        List<Items> items = itemsService.selectList(new EntityWrapper<Items>().eq("orgId", currentUser.getOrgId()).eq("deleted", false));  //所有的服务项目
        map.put("items",items);
        map.put("meal",meal);
        map.put("mealItemsIds",mealItemsIds);
        if(currentUser.getRoleId()==1){//民办
            map.put("price",1);
        }else{
            map.put("price",0);
        }
        return "services/meal/edit";
    }

    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse listUI(int page, int limit){
        Page<Meal> mealPage = mealService.selectPage(new Page<Meal>(page, limit),new EntityWrapper<Meal>().eq("orgId",currentUser.getOrgId()).eq("deleted",false).orderBy("createdTime",false));
        return new LayTableResponse(mealPage.getRecords(),mealPage.getTotal());
    }

    @RequestMapping("/save")
    public String save(Meal meal,Long[] itemsId,String[] itemsName,String[] itemsPrice){
        meal.setOrgId(currentUser.getOrgId());
        List<MealItems> mealItemList = new ArrayList<>();
        if(null != itemsId){
            for(int i = 0; i<itemsId.length;i++){
                MealItems mealItems = new MealItems(itemsName[i], itemsId[i], -1, -1, currentUser.getOrgId());
                mealItemList.add(mealItems);
            }
        }
        mealService.save(meal,mealItemList);
        return "redirect:/meal";
    }

    @RequestMapping("/update")
    public String update(Meal meal,Long[] itemsId,String[] itemsName,String[] itemsPrice){
        meal.setOrgId(currentUser.getOrgId());
        List<MealItems> mealItemList = new ArrayList<>();
        if(null != itemsId){
            for(int i = 0; i<itemsId.length;i++){
                MealItems mealItems = new MealItems(itemsName[i], itemsId[i], -1, -1, currentUser.getOrgId());
                mealItemList.add(mealItems);
                 }
        }
        mealService.update(meal,mealItemList);
        return "redirect:/meal";
    }

    /**
     * 删除
     * @param
     * @return
     */
    @RequestMapping(path = "/delete")
    public String delete(@RequestParam("id") List<Long> ids){
        mealService.delete(ids);
        return "redirect:/meal";
    }
    /**
     * 获取老人绑定套餐信息
     *
     * @return
     */
    @RequestMapping("/selectMeals")
    @ResponseBody
    public JsonResponse selectMeals(Long itemsId) {
        int itemsId1 = mealItemsService.selectCount(new EntityWrapper<MealItems>().eq("itemsId", itemsId));
        if(itemsId1>0){
            return JsonResponse.fail("err");
        }
        return JsonResponse.success("suss");
    }
}
